//
//  YJLoginHelper.m
//  JianTingSwift
//
//  Created by zhibao on 2024/11/8.
//

#import "YJLoginHelper.h"
#import "LoginController.h"
#import <ATAuthSDK/ATAuthSDK.h>

@implementation YJLoginHelper
+ (void)loginWithViewController:(UIViewController *)viewController {
    
    if ([MYGlobal share].wechatFlag == 1) {
        [[UserInfoManager sharedManager] userInit];
        return;
    }
    [QMUITips showLoadingInView:MYAPP.window];
    TXCustomModel *model = [self createTXCustomModel];
    [[TXCommonHandler sharedInstance] getLoginTokenWithTimeout:3.0
                                                    controller:viewController
                                                         model:model
                                                      complete:^(NSDictionary *_Nonnull resultDic) {
                                                          NSString *resultCode = resultDic[@"resultCode"];
                                                          if ([resultCode isEqualToString:PNSCodeLoginControllerPresentSuccess]) {
                                                              [QMUITips hideAllTips];
                                                              DLog(@"阿里云一键登录 - 授权页拉起成功回调 - %@", resultDic);
                                                          } else if ([resultCode isEqualToString:PNSCodeLoginControllerClickCancel] ||
                                                                     [resultCode isEqualToString:PNSCodeLoginControllerClickLoginBtn] ||
                                                                     [resultCode isEqualToString:PNSCodeLoginControllerClickCheckBoxBtn] ||
                                                                     [resultCode isEqualToString:PNSCodeLoginControllerClickProtocol] ||
                                                                     [resultCode isEqualToString:PNSCodeLoginClickPrivacyAlertView] ||
                                                                     [resultCode isEqualToString:PNSCodeLoginPrivacyAlertViewClickContinue] ||
                                                                     [resultCode isEqualToString:PNSCodeLoginPrivacyAlertViewClose] ||
                                                                     [resultCode isEqualToString:PNSCodeLoginPrivacyAlertViewPrivacyContentClick]) {
                                                              DLog(@"阿里云一键登录 - 页面点击事件回调 - %@", resultDic);
                                                          } else if ([resultCode isEqualToString:PNSCodeSuccess]) {
                                                              DLog(@"阿里云一键登录 - 获取LoginToken成功回调：%@", resultDic);
                                                              NSString *tokenText = resultDic[@"token"] ?: @"";
                                                              [[HttpHelper sharedInstance] sendPost:@"api/one/click/login" parameters:@{@"accessToken" : tokenText} success:^(NSURLSessionTask * _Nonnull operation, ResponseModel * _Nonnull responseObject) {
                                                                  if (responseObject.code == 1) {
                                                                      [UserInfoManager sharedManager].iToken = responseObject.data[@"data"][@"token"];
                                                                      dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                                                                          [[UserInfoManager sharedManager] getUserInfoData:^(BOOL flag) {
                                                                              dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                                                                                  [TXCommonHandler.sharedInstance cancelLoginVCAnimated:YES complete:nil];
                                                                                  
                                                                              });
                                                                          }];
                                                                          
                                                                      });
                                                                      
                                                                  }
                                                              } failure:^(NSURLSessionTask * _Nonnull operation, NSError * _Nonnull error) {
                                                                  
                                                              }];
                                                              
                                                          } else if ([resultCode isEqualToString:PNSCodeLoginControllerClickChangeBtn]) {
                                                              DLog(@"阿里云一键登录 - 用户点击其他号码登录 - %@", resultDic);
                                                              [TXCommonHandler.sharedInstance cancelLoginVCAnimated:YES
                                                                                                           complete:^{
                                                                  [viewController.navigationController pushViewController:[[LoginController alloc] init] animated:true];
                                                                                                           }];
                                                          } else {
                                                              DLog(@"获取LoginToken或拉起授权页失败回调：%@", resultDic);
                                                              [QMUITips hideAllTips];
                                                              if (viewController.presentedViewController) {
                                                                  [viewController.presentedViewController dismissViewControllerAnimated:NO completion:nil];
                                                              }
                                                              
                                                              [viewController.navigationController pushViewController:[[LoginController alloc] init] animated:true];
                                                          }
                                                      }];
}

+ (TXCustomModel *)createTXCustomModel {
    TXCustomModel *model = [[TXCustomModel alloc] init];
    model.supportedInterfaceOrientations = UIInterfaceOrientationMaskPortrait;
    model.navColor = UIColor.whiteColor;
    model.navTitle = [[NSAttributedString alloc] initWithString:@""];
    model.navBackImage = [UIImage imageNamed:@"h_r_cancel"];
    model.privacyNavBackImage = [UIImage imageNamed:@"h_r_cancel"];
    model.logoImage = [UIImage imageNamed:@"com_icon"];
    model.numberColor = [UIColor blackColor];
    model.numberFont = [UIFont boldSystemFontOfSize:28];

    model.loginBtnText = [[NSAttributedString alloc] initWithString:@"本机号码一键登录"
                                                         attributes:@{
                                                             NSForegroundColorAttributeName : UIColor.whiteColor,
                                                             NSFontAttributeName : [UIFont boldSystemFontOfSize:18]
                                                         }];
    model.changeBtnIsHidden = NO;
    model.changeBtnTitle = [[NSAttributedString alloc] initWithString:@"其他号码登录"
                                                           attributes:@{
                                                               NSForegroundColorAttributeName : MyColor(@"cacaca"),
                                                               NSFontAttributeName : [UIFont boldSystemFontOfSize:18]
                                                           }];
    model.checkBoxIsChecked = YES;
    model.privacyOne = @[ @"《用户协议》", @"https://docs.qq.com/doc/DRmNvbVFaZm9LVG9t" ];
    model.privacyTwo = @[ @"《隐私政策》", @"https://docs.qq.com/doc/DRnhXbWpia3dHV0R5" ];
    model.privacyOperatorPreText = @"《";
    model.privacyOperatorSufText = @"》";
    model.privacyPreText = @"我已阅读并同意";
    model.privacyFont = MY_SCALE_FONT(13);

    return model;
}

@end
